package engine.loaders
{
	import away3d.animators.*;
	import away3d.animators.data.*;
	import away3d.animators.skeleton.*;
	import away3d.containers.*;
	import away3d.core.base.*;
	import away3d.core.managers.*;
	import away3d.entities.*;
	import away3d.events.*;
	import away3d.library.assets.*;
	import away3d.loaders.*;
	import away3d.loaders.misc.*;
	import away3d.loaders.parsers.*;
	import engine.core.*;
	import engine.graphics.away3d.*;
	import engine.graphics.resources.*;
	import flash.display3D.*;
	import flash.events.*;
	import flash.net.*;

	public class RxLoader3D extends ObjectContainer3D
	{
		private var _size:int;
		private var _skeleton:Skeleton;
		private var _anims:Vector.<SkeletonAnimationSequence>;
		private var _mesh:Mesh;
		private var _material:RxMaterial;
		private var _skeletonAnim:RxSkeletonAnimation;
		private var _skeletonAnimState:RxSkeletonAnimationState;
		private var _skelAnimator:SmoothSkeletonAnimator;
		private var m_jointIndices:Vector.<uint>;
		private var _globalPose:SkeletonPose;
		private var m_blob:String;
		private var m_visible:Boolean;
		public static const JOINT_L_HAND:uint = 0;
		public static const JOINT_R_HAND:uint = 1;
		public static const JOINT_L_ARM:uint = 2;
		public static const JOINT_BACK:uint = 3;
		public static const JOINT_NECK:uint = 4;
		public static const JOINT_CHEST:uint = 5;
		public static const JOINT_ROOT:uint = 6;
		public static const JOINT_L_WRIST:uint = 7;
		public static const JOINT_R_WRIST:uint = 8;
		public static const JOINT_L_ANKLE:uint = 9;
		public static const JOINT_R_ANKLE:uint = 10;
		public static const JOINT_L_SHOULDER:uint = 11;
		public static const JOINT_R_SHOULDER:uint = 12;
		public static const NUM_JOINTS:uint = 13;
		private static var s_jointNames:Vector.<Vector.<String>> = null;
		private static var s_jointNameMap:Object = null;
		private static var s_worker:RxTimerWorker = null;
		private static var s_context:Context3D = null;
		
		public function RxLoader3D()
		{
			this._anims = new Vector.<SkeletonAnimationSequence>;
			this.m_jointIndices = new Vector.<uint>(NUM_JOINTS, true);
			this.m_visible = false;
		}
		
		public function Delete() : void
		{
			this._anims = null;
			this.m_jointIndices = null;
			this._skeleton = null;
			this._mesh = null;
			this._skeletonAnim = null;
			this._skeletonAnimState = null;
			this._skelAnimator = null;
			this._globalPose = null;
			if (this._material)
			{
				this._material = null;
			}
		}
		
		public static function GetJointID(property:String) : int
		{
			if (s_jointNameMap == null)
			{
				s_jointNameMap = new Object();
				s_jointNameMap["leftHand"] = JOINT_L_HAND;
				s_jointNameMap["rightHand"] = JOINT_R_HAND;
				s_jointNameMap["leftArm"] = JOINT_L_ARM;
				s_jointNameMap["back"] = JOINT_BACK;
				s_jointNameMap["neck"] = JOINT_NECK;
				s_jointNameMap["chest"] = JOINT_CHEST;
				s_jointNameMap["pelvis"] = JOINT_ROOT;
				s_jointNameMap["leftWrist"] = JOINT_L_WRIST;
				s_jointNameMap["rightWrist"] = JOINT_R_WRIST;
				s_jointNameMap["leftAnkle"] = JOINT_L_ANKLE;
				s_jointNameMap["rightAnkle"] = JOINT_R_ANKLE;
				s_jointNameMap["leftShoulder"] = JOINT_L_SHOULDER;
				s_jointNameMap["rightShoulder"] = JOINT_R_SHOULDER;
			}
			var index:int = -1;
			if (s_jointNameMap.hasOwnProperty(property))
			{
				index = s_jointNameMap[property];
			}
			return index;
		}
		
		public function instance() : RxLoader3D
		{
			var loader:RxLoader3D = new RxLoader3D();
			this.CopyTo(loader);
			
			return loader;
		}
		
		public function CopyTo(loader:RxLoader3D) : void
		{
			var subMesh:SubMesh = null;
			var material:RxMaterial = null;
			if (this._mesh == null)
			{
				return;
			}
			loader._mesh = this.InstanceMesh(this._mesh);
			var rxMaterial:* = loader._mesh.material as RxMaterial;
			if (rxMaterial != null)
			{
				rxMaterial = rxMaterial.Instance();
				loader._mesh.material = rxMaterial;
			}
			for each (subMesh in loader._mesh.subMeshes)
			{
				
				material = subMesh.material as RxMaterial;
				if (material != null && material != rxMaterial)
				{
					//subMesh.material = material.Instance();
				}
			}
			loader.addChild(loader._mesh);
			if (this._skeleton)
			{
				loader.UpdateAnimation(this._skeleton, this.anims);
			}
		}
		
		public function UpdateAnimation(param1:Skeleton, param2:Vector.<SkeletonAnimationSequence>) : void
		{
			
		}
		
		public function load(param1:String, param2:String, param3:ParserBase = null, param4:AssetLoaderContext = null, param5:String = null) : AssetLoaderToken
		{
			var _loc_6:URLRequest = null;
			var _loc_8:AssetLoaderToken = null;
			var _loc_7:* = RxManifest.GetBlob(param1);
			if (RxManifest.GetBlob(param1) != null)
			{
				this.m_blob = _loc_7;
				_loc_6 = new URLRequest(_loc_7);
			}
			else
			{
				_loc_6 = new URLRequest(param2 + param1);
			}
			var _loc_9:* = new AssetLoader();
			_loc_8 = _loc_9.load(_loc_6, param3, param4, param5);
			this.addListeners(_loc_8);
			return _loc_8;
		}
		
		private function addListeners(param1:EventDispatcher) : void
		{
			/*
			param1.addEventListener(LoaderEvent.RESOURCE_COMPLETE, this.onResourceComplete);
			param1.addEventListener(LoaderEvent.LOAD_ERROR, this.onLoadError);
			param1.addEventListener(AssetEvent.ASSET_COMPLETE, this.onAssetComplete);
			param1.addEventListener(AssetEvent.ANIMATION_COMPLETE, this.onAssetComplete);
			param1.addEventListener(AssetEvent.ANIMATOR_COMPLETE, this.onAssetComplete);
			param1.addEventListener(AssetEvent.BITMAP_COMPLETE, this.onAssetComplete);
			param1.addEventListener(AssetEvent.CONTAINER_COMPLETE, this.onAssetComplete);
			param1.addEventListener(AssetEvent.GEOMETRY_COMPLETE, this.onAssetComplete);
			param1.addEventListener(AssetEvent.MATERIAL_COMPLETE, this.onAssetComplete);
			param1.addEventListener(AssetEvent.MESH_COMPLETE, this.onAssetComplete);
			param1.addEventListener(AssetEvent.SKELETON_COMPLETE, this.onAssetComplete);
			param1.addEventListener(AssetEvent.SKELETON_POSE_COMPLETE, this.onAssetComplete);
			*/
		}
		
		public function InstanceMesh(mesh:Mesh) : Mesh
		{
			var geometry:* = this.InstanceGeometry(mesh.geometry);
			var _loc_3:* = new Mesh(geometry, mesh.material);
			_loc_3.transform = mesh.transform;
			_loc_3.pivotPoint = mesh.pivotPoint;
			_loc_3.bounds = mesh.bounds.clone();
			_loc_3.name = mesh.name;
			var len:* = mesh.subMeshes.length;
			var i:int = 0;
			while (i < len)
			{
				
				_loc_3.subMeshes[i].material = mesh.subMeshes[i].material;
				i++;
			}
			len = mesh.numChildren;
			i = 0;
			while (i < len)
			{
				
				_loc_3.addChild(ObjectContainer3D(mesh.getChildAt(i).clone()));
				i++;
			}
			return _loc_3;
		}
		
		public function InstanceGeometry(geometry:Geometry) : Geometry
		{
			var material:* = new Geometry();
			material.animation = geometry.animation;
			var len:* = geometry.subGeometries.length;
			var i:int = 0;
			while (i < len)
			{
				
				material.addSubGeometry(geometry.subGeometries[i]);
				i++;
			}
			return material;
		}
		
		public function get anims() : Vector.<SkeletonAnimationSequence>
		{
			return this._anims;
		}
		
		public static function SetWorker(worker:RxTimerWorker) : void
		{
			s_worker = worker;
		}
		
		public function IsVisible() : Boolean
		{
			return this.m_visible;
		}
		
		public function get mesh() : Mesh
		{
			return this._mesh;
		}
		
		public function set mesh(param1:Mesh) : void
		{
			this._mesh = param1;
		}
		
		public function SetVisible(param1:Boolean) : void
		{
			this.m_visible = param1;
		}
		
		public function get skelAnimator() : SmoothSkeletonAnimator
		{
			return this._skelAnimator;
		}
		
		public function set skelAnimator(param1:SmoothSkeletonAnimator) : void
		{
			this._skelAnimator = param1;
		}
		
		public function get size() : int
		{
			return this._size;
		}
	}
}